<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let loginList = [
        { userName: 'admin', password: 'admin321' },
        { userName: 'zhangsan', password: 'zs321' },
        { userName: 'lisi', password: 'ls321' },
        { userName: 'yyqx', password: 'yyqx321' },
      ]
      let userInfo = {
        token1: {
          city: '杭州',
          money: '1000w',
          sex: '女',
          userName: 'admin',
          password: 'admin321',
        },
        token2: {
          city: '北京',
          money: '1100w',
          sex: '女',
          userName: 'zhangsan',
          password: 'zs321',
        },
        token3: {
          city: '北京',
          money: '800w',
          sex: '男',
          userName: 'lisi',
          password: 'ls321',
        },
        token4: {
          city: '天津',
          money: '600w',
          sex: '男',
          userName: 'yyqx',
          password: 'yyqx321',
        },
      }
      //登录功能，成功返回true,否则返回false
      function login(user, pass) {
        for (let i = 0; i < loginList.length; i++) {
          if (loginList[i].userName === user && loginList[i].password === pass)
            return true
        }
        return false
      }
      // 记录是否注册
      let flag = false
      while (true) {
        let username = prompt('请输入用户名')
        let password = prompt('请输入密码')
        //登录功能
        if (login(username, password)) {
          let token = 'token' + (Math.floor(Math.random() * 4) + 1)
          let str = ''
          let num = 0
          for (let a in userInfo) {
            if (a === token) {
              for (let b in userInfo[a]) {
                str += b + ':' + userInfo[a][b]
                num++
                if (num === 3) break
              }
            }
          }
          str = str + '用户名:' + username
          alert(`登录成功,具体用户信息：
          ${str}
            `)
          break
        }
        //判断注册后登录是否成功
        else if (flag && !login(username, password)) {
          alert('没有机会了')
          break
        }
        //登录失败后来注册
        else {
          let choose = +prompt(' 1. 注册 2.直接退出')
          if (choose === 1) {
            let newusername = prompt('请输入用户名')
            let newpassword = prompt('请输入密码')
            let newuser = { userName: newusername, password: newpassword }
            loginList.push(newuser)
            alert('注册成功')
            flag = true
          } else if (choose === 2) {
            break
          }
        }
      }
    </script>
  </body>
</html>
